/**
 * 
 */
package info.niwota.web.jetty;

import info.niwota.web.Debug;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.jetty.HttpConnection;
import org.mortbay.jetty.Request;
import org.mortbay.jetty.handler.AbstractHandler;

import android.content.Context;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
public class HttpAdapter extends AbstractHandler {
	
	private static final String TAG = "HttpAdapter";
	
	private HttpHandler handler = null;

	public HttpAdapter(Context ctx) {
		this.handler = new HttpHandler(ctx);
	}

	public void handle(String target, HttpServletRequest request,
			HttpServletResponse response, int dispatch) throws IOException,
			ServletException {
		Request breq = (request instanceof Request) ? (Request) request
				: HttpConnection.getCurrentConnection().getRequest();
		//Response bres = (response instanceof Response) ? (Response) response
		//		: HttpConnection.getCurrentConnection().getResponse();
		breq.setHandled(true);
		//
		if (Debug.DEBUG) {
			Log.d(TAG, "handle " + target);
		}
		handler.handle(target, request, response);
	}
}
